package com.atguigu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

//解决全局跨域问题
@Configuration
public class CrosConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        CorsConfiguration config = new CorsConfiguration();
        //允许所有的网络请求 对域名进行解除限制
        config.addAllowedOrigin("*");
        //可以携带cookie
        config.setAllowCredentials(true);
        //允许所有的请求方法
        config.addAllowedMethod("*");
        //对所有请求头中携带信息
        config.addAllowedHeader("*");

        //对所有请求url进行的一个配置 放开相关跨域拦截
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**",config);
        return new CorsWebFilter(configSource);
    }
}
